#include "traceeditor.h"

#include <QInputDialog>

TraceEditor::TraceEditor(QWidget *parent) :
    QPlainTextEdit(parent), spinFilter(false)
{
}

void TraceEditor::keyPressEvent(QKeyEvent *e)
{
    int key = e->key();
    Qt::KeyboardModifiers mod = e->modifiers();

    if(mod & Qt::CTRL) {
        if(key == Qt::Key_P) { // full pasm
            setEnablePasmFilter();
        }
        if(key == Qt::Key_G) { // GCC CMM only
            setEnableCmmFilter(true);
        }
        if(key == Qt::Key_L) { // GCC LMM only
            setEnableLmmFilter(true);
        }
        if(key == Qt::Key_S) { // spin only
            setEnableSpinFilter(true);
        }
        if(key == Qt::Key_F) {
            findText();
        }
        if(key == Qt::Key_B || key == Qt::Key_R) {
            findText(QTextDocument::FindBackward);
        }
    }
    else if(key == Qt::Key_F3) {
        if(findString.length() > 0) {
            this->find(findString,findType);
        }
    }
}

void TraceEditor::findText(QTextDocument::FindFlags flags)
{
    findType = flags;
    findString = QInputDialog::getText(this,tr("Find"),tr("Find Text"));
    if(findString.length() == 0)
        return;
    this->find(findString,findType);
}

void TraceEditor::setEnablePasmFilter()
{
    spinFilter = false;
    gccCmmFilter = false;
    gccLmmFilter = false;
}

void TraceEditor::toggleSpinFilter()
{
    spinFilter = !spinFilter;
}

void TraceEditor::setEnableSpinFilter(bool enable)
{
    spinFilter = enable;
}

void TraceEditor::setEnableCmmFilter(bool enable)
{
    gccCmmFilter = enable;
}

void TraceEditor::setEnableLmmFilter(bool enable)
{
    gccLmmFilter = enable;
}

void TraceEditor::appendPlainText(const QString &text)
{
    if(spinFilter) {
        if(!text.contains("<<SPIN>>")) return;
    }
    if(gccCmmFilter) {
        if(!text.contains("<<CMM>>")) return;
    }
    if(gccLmmFilter) {
        if(!text.contains("<<LMM>>")) return;
    }
    QPlainTextEdit::appendPlainText(text);
}
